from app import db
from datetime import datetime

class ArticleCategory(db.Model):
    """文章分类模型"""
    __tablename__ = 'article_categories'
    
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False, comment='分类名称')
    description = db.Column(db.Text, comment='分类描述')
    sort_order = db.Column(db.Integer, default=0, comment='排序')
    is_active = db.Column(db.Boolean, default=True, comment='是否激活')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 关联关系
    articles = db.relationship('Article', backref='category', lazy='dynamic')
    
    def __repr__(self):
        return f'<ArticleCategory {self.name}>'
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'name': self.name,
            'description': self.description,
            'sort_order': self.sort_order,
            'is_active': self.is_active,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        }

class Article(db.Model):
    """文章模型"""
    __tablename__ = 'articles'
    
    id = db.Column(db.Integer, primary_key=True)
    category_id = db.Column(db.Integer, db.ForeignKey('article_categories.id'), nullable=False, comment='分类ID')
    title = db.Column(db.String(200), nullable=False, comment='文章标题')
    content = db.Column(db.Text, nullable=False, comment='文章内容')
    summary = db.Column(db.Text, comment='文章摘要')
    cover_image = db.Column(db.Text, comment='封面图片URL')
    view_count = db.Column(db.Integer, default=0, comment='浏览次数')
    like_count = db.Column(db.Integer, default=0, comment='点赞数')
    collect_count = db.Column(db.Integer, default=0, comment='收藏数')
    is_published = db.Column(db.Boolean, default=False, comment='是否发布')
    published_at = db.Column(db.DateTime, comment='发布时间')
    created_by = db.Column(db.Integer, db.ForeignKey('admins.id'), nullable=False, comment='创建者ID')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 关联关系
    user_likes = db.relationship('UserArticleLike', backref='article', lazy='dynamic')
    user_collections = db.relationship('UserArticleCollection', backref='article', lazy='dynamic')
    
    def __repr__(self):
        return f'<Article {self.title}>'
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'category_id': self.category_id,
            'title': self.title,
            'content': self.content,
            'summary': self.summary,
            'cover_image': self.cover_image,
            'view_count': self.view_count,
            'like_count': self.like_count,
            'collect_count': self.collect_count,
            'is_published': self.is_published,
            'published_at': self.published_at.isoformat() if self.published_at else None,
            'created_by': self.created_by,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        }
    
    def increment_view_count(self):
        """增加浏览次数"""
        self.view_count += 1
        db.session.commit() 